package com.wjh.util;

import java.util.Collections;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson2.JSON;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wenjianhai
 * @date 2023/6/10
 * @since JDK 1.8
 */
@Slf4j
@Component
public class RestTemplateUtil {

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * GET请求
	 *
	 * @date 2023-06-10
	 * @param url    请求地址
	 * @param params 请求参数
	 * @return String
	 * @since JDK 1.8
	 * @author wenjianhai
	 */
	public String get(String url, Map<String, Object> params) {
		log.info("RestTemplate Get请求-开始.url:{}, 请求参数:{}", url, JSON.toJSONString(params));
		try {
			ResponseEntity<String> response = null;

			if (MapUtils.isEmpty(params)) {
				response = restTemplate.getForEntity(url, String.class);
			} else {
				response = restTemplate.getForEntity(url, String.class, params);
			}
			log.info("RestTemplate Get请求-结束.url:{}, 请求参数:{}, 返回结果:{}", url, JSON.toJSONString(params),
					JSON.toJSONString(response));

			if (HttpStatus.OK.value() != response.getStatusCodeValue()) {
				log.error("RestTemplate Get请求-失败.url:{}, 请求参数:{}, 返回结果:{}", url, JSON.toJSONString(params),
						JSON.toJSONString(response));
				throw new RuntimeException("请求失败");
			}
			return response.getBody();
		} catch (Exception e) {
			log.error(String.format("RestTemplate Get请求-失败.url:%s, 请求参数:%s", url, JSON.toJSONString(params)), e);
			throw new RuntimeException("请求失败");
		}
	}

	/**
	 * post json 请求
	 *
	 * @date 2023-06-10
	 * @param url    请求地址
	 * @param bodyParams 请求体参数
	 * @param headerParams 请求头参数
	 * @return String
	 * @since JDK 1.8
	 * @author wenjianhai
	 */
	public String postJson(String url, Map<String, Object> bodyParams, Map<String, Object> headerParams) {
		log.info("RestTemplate Post Json请求-开始.url:{}, 请求体参数:{}, 请求头参数:{}", url, JSON.toJSONString(bodyParams),
				JSON.toJSONString(headerParams));
		try {
			// 请求头
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_JSON);
			headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
			if (MapUtils.isNotEmpty(headerParams)) {
				for (Map.Entry<String, Object> entry : headerParams.entrySet()) {
					headers.set(entry.getKey(), entry.getValue().toString());
				}
			}
			HttpEntity<Map<String, Object>> entity = new HttpEntity<>(bodyParams, headers);

			ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);

			log.info("RestTemplate Post Json请求-结束.url:{}, 请求体参数:{}, 请求头参数:{}, 返回结果:{}", url,
					JSON.toJSONString(bodyParams), JSON.toJSONString(headerParams), JSON.toJSONString(response));

			if (HttpStatus.OK.value() != response.getStatusCodeValue()) {
				log.error("RestTemplate Post Json请求-失败.url:{}, 请求体参数:{}, 请求头参数:{}, 返回结果:{}", url,
						JSON.toJSONString(bodyParams), JSON.toJSONString(headerParams), JSON.toJSONString(response));
				throw new RuntimeException("请求失败");
			}
			return response.getBody();
		} catch (Exception e) {
			log.error(String.format("RestTemplate Post Json请求-失败.url:%s, 请求体参数:%s, 请求头参数:%s", url,
					JSON.toJSONString(bodyParams), JSON.toJSONString(headerParams)), e);
			throw new RuntimeException("请求失败");
		}
	}

	/**
	 * post form 请求
	 *
	 * @date 2023-06-10
	 * @param url    请求地址
	 * @param params 请求参数
	 * @return String
	 * @since JDK 1.8
	 * @author wenjianhai
	 */
	public String postForm(String url, Map<String, Object> params) {
		log.info("RestTemplate Post Form请求-开始.url:{}, 请求参数:{}", url, JSON.toJSONString(params));
		try {
			// 请求头
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

			HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers);

			ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);

			log.info("RestTemplate Post Form请求-结束.url:{}, 请求参数:{}, 返回结果:{}", url, JSON.toJSONString(params),
					JSON.toJSONString(response));

			if (HttpStatus.OK.value() != response.getStatusCodeValue()) {
				log.error("RestTemplate Post Form请求-失败.url:{}, 请求参数:{}, 返回结果:{}", url, JSON.toJSONString(params),
						JSON.toJSONString(response));
				throw new RuntimeException("请求失败");
			}
			return response.getBody();
		} catch (Exception e) {
			log.error(String.format("RestTemplate Post Form请求-失败.url:%s, 请求参数:%s", url, JSON.toJSONString(params)), e);
			throw new RuntimeException("请求失败");
		}
	}
}
